package com.baosight.risk.service.system.impl;

import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.mapper.xtzy.XtsqMenuAuthoMapper;
import com.baosight.risk.service.system.entity.SysRole;
import com.baosight.risk.mapper.system.SysRoleMapper;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.api.system.ISysRoleService;
import com.baosight.risk.service.system.vo.SysRoleVO;
import com.baosight.risk.service.xtzy.entity.XtsqMenuAutho;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private XtsqMenuAuthoMapper menuAuthoMapper;

    @Override
    public Integer insertRole(List<SysRole> roleList) throws Exception {
        for (SysRole role : roleList) {
            if (StringUtils.isNotEmpty(role.getId())) {
                roleMapper.updateByPrimaryKeySelective(role);
            } else {
                //数据校验
                List<SysRole> list = roleMapper.checkRole(role.getRoleKey(), null);
                if (CollectionUtils.isNotEmpty(list)) {
                    throw new Exception(role.getRoleKey() + "==角色编码已经存在!");
                }
                list = roleMapper.checkRole(null, role.getRoleName());
                if (CollectionUtils.isNotEmpty(list)) {
                    throw new Exception(role.getRoleName() + "==角色名称已经存在!");
                }
                role = (SysRole) BeanUtils.initBean(role);
                role.setStatus("10");
                role.setDelFlag("0");
                roleMapper.insert(role);
            }
        }
        return 1;
    }

    @Override
    public Set<String> selectRoleKeys(String userId) {
        List<SysRole> perms = roleMapper.selectRolesByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms) {
            if (StringUtils.isNotNull(perms)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysRole> selectRole(SysRole role) {
        return roleMapper.selectRole(role);
    }

    @Override
    public Integer authorization(SysRoleVO roleVO) {
        List<Map<String, String>> menu = JSONArray.fromObject(roleVO.getMenuMap());
        List<Map<String, String>> comp = JSONArray.fromObject(roleVO.getCompMap());
        //现根据角色找数据，如果有删掉后在新增
        Integer count = menuAuthoMapper.selectRoleCode(roleVO.getRoleCode());
        if (count > 0) {
            menuAuthoMapper.deleteByRoleCode(roleVO.getRoleCode());
        }
        //新增关联关系
        for (Map<String, String> compMap : comp) {
            for (Map<String, String> menuMap : menu) {
                XtsqMenuAutho menuAutho = new XtsqMenuAutho();
                menuAutho.setCompCode(compMap.get("compCode"));
                menuAutho.setCompName(compMap.get("compName"));
                menuAutho.setMenuCode(menuMap.get("menuCode"));
                menuAutho.setMenuName(menuMap.get("menuName"));
                menuAutho.setRemark(menuMap.get("remark"));
                menuAutho.setRoleCode(roleVO.getRoleCode());
                menuAutho.setRoleName(roleVO.getRoleName());
                menuAutho.setRowOrder(menuMap.get("rowOrder"));
                menuAutho = (XtsqMenuAutho) BeanUtils.initBean(menuAutho);
                menuAuthoMapper.insert(menuAutho);
            }
        }
        return 1;
    }

    @Override
    public Integer updateRole(SysRole role) {
        return roleMapper.updateByPrimaryKeySelective(role);

    }

    @Override
    public List<SysRole> selectRoleByIds(List<String> ids) {
        return roleMapper.selectRoleByIds(ids);
    }

    @Override
    public Integer delelteRoleByIds(List<String> ids, String status) {
        return roleMapper.delelteRoleByIds(ids, status);
    }
}
